home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / DYNAMENU.PAK / DYNMNU.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.1 KB  |  301 lines

  1. // ---------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (C) 1995, 1995 by Borland International, All Rights Reserved
  4. // ---------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include "dynmnu.h"
  7.  
  8. //
  9. // constructor
  10. //
  11. TDynamicMenuAttr::TDynamicMenuAttr(const string itemString,
  12.   const string hintText, int id)
  13. :
  14.   ItemString(itemString),
  15.   HintText(hintText),
  16.   ItemId(id),
  17.   Enabled(true)
  18. {
  19. }
  20.  
  21.  
  22. //
  23. // destructor
  24. //
  25. TDynamicMenuAttr::~TDynamicMenuAttr()
  26. {
  27. }
  28.  
  29.  
  30. //
  31. // MenuTestWindow constructor
  32. //
  33. TMenuTestWindow::TMenuTestWindow()
  34. :
  35.   TWindow(0, 0, 0)
  36. {
  37.   NumMenuItems = 0;
  38.   for (int i = 0; i < NUMDYNMENUITEMS; i++)
  39.     DynamicMenuAttr[i] = 0;
  40. }
  41.  
  42.  
  43. //
  44. // MenuTestWindow destructor
  45. //
  46. TMenuTestWindow::~TMenuTestWindow()
  47. {
  48.   for (int i = 0; i < NUMDYNMENUITEMS; i++)
  49.     delete DynamicMenuAttr[i];
  50. }
  51.  
  52.  
  53. //
  54. // Response table for MenuTestWindow
  55. //
  56. DEFINE_RESPONSE_TABLE1(TMenuTestWindow, TWindow)
  57.   EV_COMMAND(CM_ADDMENU, CmAddMenu),
  58.   EV_COMMAND       (CM_DELETEMENU, CmDeleteMenu),
  59.   EV_COMMAND_ENABLE(CM_DELETEMENU, CeDeleteMenu),
  60.   EV_COMMAND       (CM_ENABLEMENU, CmEnableMenu),
  61.   EV_COMMAND_ENABLE(CM_ENABLEMENU, CeEnableMenu),
  62.   EV_COMMAND       (CM_DISABLEMENU, CmDisableMenu),
  63.   EV_COMMAND_ENABLE(CM_DISABLEMENU, CeDisableMenu),
  64.   EV_WM_RBUTTONDOWN,
  65. END_RESPONSE_TABLE;
  66.  
  67.  
  68. //
  69. // GetIndex
  70. //
  71. int
  72. TMenuTestWindow::GetIndex(int low, int high)
  73. {
  74.   const int bufferSize = 80;
  75.   char buffer[bufferSize];
  76.  
  77.   lstrcpy(buffer, "");
  78.   if (TInputDialog(this, "", "Enter the menu id (between 10000-19999):",
  79.     buffer, bufferSize).Execute() != IDOK) {
  80.     MessageBox("Cancelled", "Status", MB_OK);
  81.     throw IDCANCEL;
  82.   }
  83.  
  84.   int id = atoi(buffer);
  85.   if (id < low || high <= id) {
  86.     MessageBox("Number not in range", "Error", MB_OK | MB_ICONSTOP);
  87.     throw IDCANCEL;
  88.   }
  89.  
  90.   // got a valid id
  91.   //
  92.   return id - low;
  93. }
  94.  
  95.  
  96. //
  97. // Add a new menu item
  98. //
  99. void
  100. TMenuTestWindow::CmAddMenu()
  101. {
  102.   const int bufferSize = 80;
  103.   char menuString[bufferSize];
  104.   char hintText[bufferSize];
  105.   int index, id;
  106.  
  107.   try {
  108.     index = GetIndex(CM_DYNFIRST, CM_DYNLAST);
  109.     id = index + CM_DYNFIRST;
  110.   }
  111.   catch (int) {
  112.     return;   // invalid input
  113.   }
  114.  
  115.   if (DynamicMenuAttr[index] != 0) {
  116.     MessageBox("Id already in use", "Error", MB_OK | MB_ICONSTOP);
  117.     return;
  118.   }
  119.  
  120.   lstrcpy(menuString, "");
  121.   if (TInputDialog(this, "", "Enter the menu string:",
  122.     menuString, bufferSize).Execute() != IDOK) {
  123.     MessageBox("Cancelled", "Status", MB_OK);
  124.     return; // not a valid menu id
  125.   }
  126.  
  127.   lstrcpy(hintText, "");
  128.   if (TInputDialog(this, "", "Enter the hint text:",
  129.     hintText, bufferSize).Execute() != IDOK) {
  130.     MessageBox("Cancelled", "Status", MB_OK);
  131.     return; // not a valid menu id
  132.   }
  133.  
  134.   DynamicMenuAttr[index] = new TDynamicMenuAttr(menuString, hintText, id);
  135.  
  136.   TMenu* menu = new TMenu(*GetApplication()->GetMainWindow());
  137.   TMenu* dynMenu = new TMenu(menu->GetSubMenu(1));
  138.  
  139.   dynMenu->InsertMenu(NumMenuItems, MF_BYPOSITION, id, menuString);
  140.  
  141.   delete dynMenu;
  142.   delete menu;
  143.  
  144.   NumMenuItems++;
  145. }
  146.  
  147. //
  148. // Remove a menu item
  149. //
  150. void
  151. TMenuTestWindow::CmDeleteMenu()
  152. {
  153.   int index;
  154.  
  155.   try {
  156.     index = GetIndex(CM_DYNFIRST, CM_DYNLAST);
  157.   }
  158.   catch (int) {
  159.     return;   // invalid input
  160.   }
  161.  
  162.   if (DynamicMenuAttr[index] == 0) {
  163.     MessageBox("Menu item not defined", "Error", MB_OK | MB_ICONSTOP);
  164.     return;
  165.   }
  166.  
  167.   TMenu* menu = new TMenu(*GetApplication()->GetMainWindow());
  168.   TMenu* dynMenu = new TMenu(menu->GetSubMenu(1));
  169.  
  170.   dynMenu->DeleteMenu(index + CM_DYNFIRST, MF_BYCOMMAND);
  171.  
  172.   delete dynMenu;
  173.   delete menu;
  174.  
  175.   NumMenuItems--;
  176. }
  177.  
  178. void
  179. TMenuTestWindow::CeDeleteMenu(TCommandEnabler& ce)
  180. {
  181.   ce.Enable(NumMenuItems != 0);
  182. }
  183.  
  184. //
  185. // Enable a menu item
  186. //
  187. void TMenuTestWindow::CmEnableMenu()
  188. {
  189.   int index;
  190.  
  191.   try {
  192.     index = GetIndex(CM_DYNFIRST, CM_DYNLAST);
  193.   }
  194.   catch (int) {
  195.     return;   // invalid input
  196.   }
  197.  
  198.   if (DynamicMenuAttr[index] == 0) {
  199.     MessageBox("Menu item not defined", "Error", MB_OK | MB_ICONSTOP);
  200.     return;
  201.   }
  202.  
  203.   DynamicMenuAttr[index]->Enable(true);
  204. }
  205.  
  206. void
  207. TMenuTestWindow::CeEnableMenu(TCommandEnabler& ce)
  208. {
  209.   ce.Enable(NumMenuItems != 0);
  210. }
  211.  
  212. //
  213. // Disable a menu item
  214. //
  215. void TMenuTestWindow::CmDisableMenu()
  216. {
  217.   int index;
  218.  
  219.   try {
  220.     index = GetIndex(CM_DYNFIRST, CM_DYNLAST);
  221.   }
  222.   catch (int) {
  223.     return;   // invalid input
  224.   }
  225.  
  226.   if (DynamicMenuAttr[index] == 0) {
  227.     MessageBox("Menu item not defined", "Error", MB_OK | MB_ICONSTOP);
  228.     return;
  229.   }
  230.  
  231.   DynamicMenuAttr[index]->Enable(false);
  232. }
  233.  
  234. void
  235. TMenuTestWindow::CeDisableMenu(TCommandEnabler& ce)
  236. {
  237.   ce.Enable(NumMenuItems != 0);
  238. }
  239.  
  240.  
  241. //
  242. // override TWindow's virtual functions
  243. // EvCommand
  244. //
  245. LRESULT
  246. TMenuTestWindow::EvCommand(uint id, HWND hWndCtl, uint notifyCode)
  247. {
  248.   if (CM_DYNFIRST <= id && id < CM_DYNLAST) {
  249.     // execute dynamic menu specific item
  250.     //
  251.     int index = id - CM_DYNFIRST;
  252.     string output = "You've selected " + DynamicMenuAttr[index]->GetItemString();
  253.     MessageBox(output.c_str(), "Command", MB_OK);
  254.     return 0;
  255.   }
  256.   return TWindow::EvCommand(id, hWndCtl, notifyCode);
  257. }
  258.  
  259.  
  260. //
  261. // EvCommandEnable
  262. //
  263. void
  264. TMenuTestWindow::EvCommandEnable(TCommandEnabler& ce)
  265. {
  266.   if (CM_DYNFIRST <= ce.Id && ce.Id < CM_DYNLAST) {
  267.     // enable dynamic menu specific item
  268.     //
  269.     int index = ce.Id - CM_DYNFIRST;
  270.     ce.Enable(DynamicMenuAttr[index]->IsEnabled());
  271.   }
  272.   else
  273.     TWindow::EvCommandEnable(ce);
  274. }
  275.  
  276.  
  277.  
  278. //
  279. // WM_RBUTTONDOWN
  280. //
  281. void
  282. TMenuTestWindow::EvRButtonDown(uint, TPoint& point)
  283. {
  284.   HWND hwndFrame = *GetApplication()->GetMainWindow();
  285.  
  286.   TMenu* menu           = new TMenu(hwndFrame);
  287.   TMenu* dynMenu        = new TMenu(menu->GetSubMenu(1));
  288.   TPopupMenu* popupMenu = new TPopupMenu(*dynMenu);
  289.  
  290.   ClientToScreen(point);
  291.   // send menu messages to the main window allow menu tracking to work seemlessly
  292.   //
  293.   popupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
  294.     point, 0, hwndFrame, 0);
  295.  
  296.   delete popupMenu;
  297.   delete dynMenu;
  298.   delete menu;
  299. }
  300.  
  301.